package com.ruoyi.back.service.impl

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Ok
import com.ruoyi.app.common.PageData
import com.ruoyi.app.common.pageInfo
import com.ruoyi.back.domain.TemplateCategory
import com.ruoyi.back.mapper.TemplateCategoryMapper
import com.ruoyi.back.service.TemplateCategoryService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.stereotype.Service
import java.util.*

@Service
class TemplateCategoryServiceImpl : ServiceImpl<TemplateCategoryMapper, TemplateCategory>(), TemplateCategoryService {


    override fun saveTemplateCategory(templateCategory: TemplateCategory): Data<Boolean> =
        templateCategory.id?.run {
            Ok("更新模板分类成功", updateById(templateCategory))
        } ?: kotlin.run {
            Ok("保存模板分类成功", save(templateCategory.apply { createTime = Date() }))
        }

    override fun getNameById(id: Long): String? =
        getById(id)?.name

    override fun isTest(id: Long?): Boolean =
        getById(id)?.run {
            name == "体验"
        } ?: false

    override fun templateCategoryPage(templateCategory: TemplateCategory): PageData<TemplateCategory> =
        TableSupport.buildPageRequest().let { pageDomain ->
            page(Page(pageDomain.pageNum.toLong(), pageDomain.pageSize.toLong()), KtQueryWrapper(TemplateCategory::class.java)
                .eq(templateCategory.type != null, TemplateCategory::type, templateCategory.type)
                .orderByAsc(TemplateCategory::orderNum)).pageInfo()
        }

    override fun removeTemplateCategory(ids: String): Data<Boolean> =
        Ok("移除成功", removeByIds(ids.split(",")))
}